<?php

class IOSS_AffiliateWithdraw{
	private static $table = 'core_affiliate_withdraw';
	/** 用户提款未支付 */
	const STATE_NOT_PAID = 0;
	/** 用户提款已支付 */
	const STATE_PAID = 1;
	/** 用户提款已取消 */
	const STATE_CANCELLED = -1;

	public static $status = Array(self::STATE_NOT_PAID=>'审核中', self::STATE_PAID=>'已确认',self::STATE_CANCELLED=>'已取消');

	/**
	 * 取款记录请求
	 * @param string $user_id
	 * @param int $last_id
	 */
	public static function getWithdrawList($user_id, $last_id=null, $limit=null){
		$db = IOSS_DB::getInstance();
		$db->select(array('id','amount as price','status', 'FROM_UNIXTIME(create_time,"%Y-%m-%d %H:%i:%S") as date'));
		$db->from(self::$table);
		$db->where('user_id',$user_id);
		$db->order_by('id','DESC');
		if($last_id !== null){
			$db->where('id<',$last_id);
		}
		if(is_numeric($limit)){
			$db->limit($limit);
		}
		$query = $db->get();
		return $query->result();
	}


	/**
	* 获取给定用户所有正在入账提成的总和
	*/
	public static function getCountDownTotalMoney($user_id){
		$db = IOSS_DB::getInstance();
		$db->select('SUM(commissions_lv1) as lv1_total');
		$db->from('core_game_record');
		$db->where('status',0);
		$db->where('p1_user_id',$user_id);		//暂时测试用户5
		$lv1_total_money = $db->get()->result();

		$db = IOSS_DB::getInstance();
		$db->select('SUM(commissions_lv2) as lv2_total');
		$db->from('core_game_record');
		$db->where('status',0);
		$db->where('p2_user_id',$user_id);		//暂时测试用户1
		$lv2_total_money = $db->get()->result();

		return $lv1_total_money[0]->lv1_total + $lv2_total_money[0]->lv2_total;
	}

	/**
	 * 提成记录待入账明细
	 * @param string $user_id
	 */
	public static function getShareDetail($user_id, $last_id=null, $limit=null){
		$db = IOSS_DB::getInstance();
		$db->select('g.*, u.account')->from('core_game_record g')->join('core_user u','g.user_id=u.id','LEFT')
			->where('g.status',self::STATE_NOT_PAID)
			->group_start()->where('p1_user_id', $user_id)->or_where('p2_user_id', $user_id)->group_end()
			->order_by('g.id','DESC');
		if(is_numeric($last_id)){
			$db->where('g.id <', $last_id);
		}
		if(is_numeric($limit)){
			$db->limit($limit);
		}
		return $db->get()->result();
	}

	private static function cmpobjectfield($a,$b){
		return strcmp($b->id, $a->id);
	}

	/**
	 * 创建返利余额提现记录
	 */
	public static function create($user_id, $amount, $bank_id){
		$db = IOSS_DB::getInstance();
		$time = time();
		$o['user_id'] = $user_id;
		$o['amount'] = $amount;
		$o['status'] = self::STATE_NOT_PAID;
		$o['bank_id'] = $bank_id;
		$o['create_time'] = $time;
		$o['update_time'] = $time;
		$o['bank_transaction_id'] = '';
		$o['bank_transaction_time'] = '';
		$try_times = 0;
		do{
			if($try_times > 10){
				return  false; //重试次数太多，创建订单失败
			}
			$try_times ++;
			$o['no'] = date('ymdHis') . rand(1000, 9999);
			$unique = $db->get_where(self::$table, array('no' => $o['no']))->num_rows() === 0;
		}while($unique === false);
	
		if($db->insert(self::$table, $o)){
			$o['id'] =  $db->insert_id();

			// 修改用户表commissions数据
			$db->where('id',$user_id);
			$db->set('commissions', 'commissions-'.$amount, FALSE);
			$db->update('user');	

			return $o;
		}else{
			return false;
		}
	}
}